
package com.sfdlJPush.controller.api;

import android.view.View;
import android.widget.Toast;

import com.sfdlJPush.controller.MainApplication;
import com.sfdlJPush.widget.pulltorefresh.PullToRefreshBase;

public abstract class ApiListenrDefaultImpl implements ApiListener
{
    private View view;
    private B5MBaseResponse b5mBaseResponse;

    public B5MBaseResponse getB5mBaseResponse()
    {
        return b5mBaseResponse;
    }

    public void setB5mBaseResponse(B5MBaseResponse b5mBaseResponse)
    {
        this.b5mBaseResponse = b5mBaseResponse;
    }

    public View getView()
    {
        return view;
    }

    /**
     * 每一个request对应一个view，默认请求对应head处的
     * 
     * @param view
     */
    public void setView(View view)
    {
        this.view = view;
    }

    /**
     * 处理HttpStatus.SC_OK的情况
     */
    @Override
    public void onSuccess(B5MBaseResponse b5mBaseResponse)
    {
        // TODO Auto-generated method stub
        onFinish();
    };

    /**
     * 处理其他各种异常情况
     */
    @Override
    public void onFail(int errorCode , String errorMsg)
    {
        // TODO Auto-generated method stub
        Toast.makeText(MainApplication.getInstance(), errorMsg, Toast.LENGTH_SHORT).show();// 默认显示错误消息
        onFinish();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Override
    public void onFinish()
    {
        // TODO Auto-generated method stub
        if (view != null)
        {
            view.setVisibility(View.GONE);
        }
        if (b5mBaseResponse instanceof B5MBasePaggingResponse)
        {
            B5MBasePaggingResponse b5mBasePaggingResponse = (B5MBasePaggingResponse) b5mBaseResponse;
            PullToRefreshBase<View> pullToRefreshBase = b5mBasePaggingResponse.getPullToRefreshBase();
            if (pullToRefreshBase != null)
            {
                pullToRefreshBase.onRefreshComplete();
            }
        }
    }
}